import java.io.*;
import java.util.*;
public class exec_rmdirs {

	public static void main(String[] args) throws Exception{
		System.out.println("usage: java exec_rmdirs <option- 1/2> [<skiplines> <sleep1> <sleep2> <minlengthinkb> <bytearraylength> <pattern>]");
		String dirname="rm1.sh";
		if(args.length<1){
			System.out.println("specify '1' or '2' : \"one way dir -1 (rm1.sh -default) or 2 way dir -2(rm2_unsorted.sh) \"");
			System.exit(0);
		}else {
			if(args[0].equals("2"))
				dirname="rm2_unsorted.sh";
			
		}

		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(dirname)));
		String line="";
		int skip_lines=0;
		int sleep=10;
		int sleep2=0;
		int minlength_in_kb=100;
		int barrlen=100000;
		String pattern="";
		if(args.length >1)
			skip_lines=Integer.parseInt(args[1]);
		if(args.length >2)
			sleep=Integer.parseInt(args[2]);
		if(args.length >3)
			sleep2=Integer.parseInt(args[3]);
		if(args.length >4)
			minlength_in_kb=Integer.parseInt(args[4]);
		if(args.length >5)
			barrlen=Integer.parseInt(args[5]);
		if(args.length >6)
			pattern=args[6];
		System.out.println("sleep="+sleep);
		int _cnt=0;
		while((line=br.readLine())!= null){
			_cnt++;
			if(_cnt<skip_lines){
				continue;
			}
			if(!line.toLowerCase().endsWith(pattern.toLowerCase()+"\""))
				continue;
			if(!line.startsWith("meld \""))
				continue;
			System.out.println("(("+_cnt+"))");
			line=line.substring("meld \"".length());
			int len=line.length();
			int i=line.indexOf("\" \"");
			int j=line.indexOf("\"",i+3);
			if(i<5 || j< 9 || (j!=(len-1))){
				System.out.println("\t\terr line>>\""+line+"\"");
				System.out.println("i="+i);
				System.out.println("j="+j);
				System.out.println("len="+len);
				Thread.sleep(1000000);
				continue;
			}
			String args0=line.substring(0,i);
			String args1=line.substring(i+3,j);
			File f0=new File(args0);
			File f1=new File(args1);
			if(! f0.exists()){
				System.out.println("\n\nnot existing file1==\""+args0+"\"\n\n");
				continue;
			}
			if(! f1.exists()){
				System.out.println("\n\nnot existing file2==\""+args1+"\"\n\n");
				continue;
			}
			if(f0.isDirectory() && f1.isDirectory()){
					System.out.println("\tdiff \""+args0+"\" \""+args1+"\"\n");
					System.out.println("ls -l \""+args0+"\"");
					System.out.println("ls -l \""+args1+"\"\n\n");
					System.out.println("filename=1. \""+args0.substring(args0.lastIndexOf("/")+1)+"\"");
					System.out.println("filename=2. \""+args1.substring(args1.lastIndexOf("/")+1)+"\"\n\n-----------------");
					if(args0.trim().equals(args1.trim()) || args0.equals(args1)|| args0.trim().equals(args1) || args0.equals(args1.trim())){
						System.out.println("ERRRRRROOOOOORRRRRRRRRRRRRRRRRRRRRRRRRRRRRR");
						continue;
					}
					Date dt1 = new Date();
					int _cmpres=SuperDir.compareDir(f0, f1);
					if( _cmpres==0 ||_cmpres==1000){
						Date dt2 = new Date();
						System.out.println("filename=-----------"+f0.length()+';'+((dt2.getTime()-dt1.getTime())*1000000L)/f0.length());
						System.out.println("diff match(removing seond):"+_cmpres);
						System.out.println("\t1.\""+args0+"\"");
						System.out.println("\t2.\""+args1+"\"\n");
						Thread.sleep(1000*sleep);
						Process pc=Runtime.getRuntime().exec(new String[]{"rm","-rf",args1});
						int iii=pc.waitFor();
						System.out.println("\nremoved seond:"+args1+"="+iii);
						System.out.println("-----------\n\n\n");
						Thread.sleep(sleep2*1000);
					}else{
						System.out.println("\tdiff MISMATCH\n");
						
					}
			}else{
				System.out.println("\t\terr not dirs ==\""+args0+"\":\""+args1+"\"");
				Thread.sleep(10000);
				continue;
			}
			
			
		}
	}

}
